package com.example.jiemian;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.example.jiemian.R;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.view.LineChartView;

public class ceshi5 extends Fragment {

    //定义下拉框的数据
    public String[] spinnerData1=new String[]{"空气温度","数据2"};
    public String[] spinnerData2=new String[]{"一分钟","数据2"};

    //声明下拉框控件
    private Button button1;
    Spinner spinner1;
    Spinner spinner2;

    LineChartView lineChartView;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle){
        //获取主页面窗体对象
        View view=layoutInflater.inflate(R.layout.ceshi5,viewGroup,false);

        //获取窗体各个控件
        spinner1=(Spinner)view.findViewById(R.id.ceshi5_spinner_1);
        spinner2=(Spinner)view.findViewById(R.id.ceshi5_spinner_2);
        button1=(Button)view.findViewById(R.id.ceshi5_button);
        lineChartView=(LineChartView)view.findViewById(R.id.ceshi5_lecho);

        //绑定相关数据
        spinner1.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,spinnerData1));
        spinner2.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,spinnerData2));

        //给按钮添加相关事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String a = spinner1.getSelectedItem().toString();
                shouChart(a);
            }
        });

        return  view;
    }
    //数据统计分析显示
    public void  shouChart(String a){
        List<PointValue> pointValues = new ArrayList<>();

        if (a=="空气温度") {


            pointValues.add(new PointValue(0, 1));
            pointValues.add(new PointValue(1, 2));
            pointValues.add(new PointValue(6, 3));
            pointValues.add(new PointValue(3, 4));
            pointValues.add(new PointValue(0, 50));
            pointValues.add(new PointValue(5, 60));
            pointValues.add(new PointValue(2, 7));
            pointValues.add(new PointValue(7, 8));
        }else {
            pointValues.add(new PointValue(0, 10));
            pointValues.add(new PointValue(1, 20));
            pointValues.add(new PointValue(6, 30));
            pointValues.add(new PointValue(3, 4));
            pointValues.add(new PointValue(4, 50));
            pointValues.add(new PointValue(0, 60));
            pointValues.add(new PointValue(2, 70));
            pointValues.add(new PointValue(7, 80));
        }

        //设置相关的线条属性
        Line line1=new Line(pointValues).setColor(Color.GRAY);
        List<Line> lines=new ArrayList<>();
        lines.add(line1);

        LineChartData data=new LineChartData();

        data.setLines(lines);

        //设置坐标
        int[] num=new int[]{0,5,10,15,20,25,30,35};

        //y轴数据
        List<AxisValue> Y=new ArrayList<>();
        for (int i=0;i<num.length;i++){
            Y.add(new AxisValue(i).setValue(num[i]));

        }
        //x轴数据
        String[] str2=new String[]{"1小时","2小时","3小时","4小时","5小时","6小时","7小时"};
        List<AxisValue> X=new ArrayList<>();
        for (int i=0;i<str2.length;i++){
            X.add(new AxisValue(i).setLabel(str2[i]));
        }
        //设置坐标名称
        Axis axisX=new Axis();
        Axis axisY=new Axis();

        axisX.setName("时间");
        axisY.setName("数值");

        data.setAxisXBottom(axisX);
        data.setAxisYLeft(axisY);

        axisX.setValues(X);
        axisY.setValues(Y);

        lineChartView.setLineChartData(data);


    }



}
